<!DOCTYPE html>
<html>
<head>
  <title>type类型判断</title>
</head>
<body>
<div id='container' style="height:300px;width:100%;background-color:black;color: white;font-size:23px;text-align: center;line-height: 300px;">
  0
</div>
<script type="text/javascript">

  // 参考：https://github.com/mqyqingfeng/Blog/blob/master/articles/%E4%B8%93%E9%A2%98%E7%B3%BB%E5%88%97%E6%96%87%E7%AB%A0/JavaScript%E4%B8%93%E9%A2%98%E4%B9%8B%E7%B1%BB%E5%9E%8B%E5%88%A4%E6%96%AD(%E4%B8%8A).md
  //

  // 以下是14种：
  var number = 1;          // [object Number]
  var string = '123';      // [object String]
  var boolean = true;      // [object Boolean]
  var und = undefined;     // [object Undefined]
  var nul = null;          // [object Null]
  var obj = {a: 1}         // [object Object]
  var array = [1, 2, 3];   // [object Array]
  var date = new Date();   // [object Date]
  var error = new Error(); // [object Error]
  var reg = /a/g;          // [object RegExp]
  var func = function a(){}; // [object Function]
  // console.log(Object.prototype.toString.call(Math)); // [object Math]
  // console.log(Object.prototype.toString.call(JSON)); // [object JSON]
  function a() {
      console.log(Object.prototype.toString.call(arguments)); // [object Arguments]
  }
  // a();

// -------------------------推荐版（jquery版）--------------------------------------

  var class2type = {};

  // 生成class2type映射
  "Boolean Number String Function Array Date RegExp Object Error".split(" ").map(function(item, index) {
      class2type["[object " + item + "]"] = item.toLowerCase();
  })

  console.log(class2type)

  function type(obj) {
    // 一箭双雕
    if (obj == null) {
        return obj + "";
    }
    return typeof obj === "object" || typeof obj === "function" ?
        class2type[Object.prototype.toString.call(obj)] || "object" :
        typeof obj;
  }

  console.log(typeof(undefined))
</script>
</body>
</html>
